
layui.use(['form', 'layedit', 'laydate',"table",'upload'], function() {
    var $ = layui.jquery
        , upload = layui.upload;
    var table = layui.table;
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate;
    //新增按钮
    $('#add').click(function () {
        var  width=($(window).width()*0.8);
        var height=($(window).height()*0.9);
        layer.open({
            type: 1,
            closeBtn: 0,
            area: [width + 'px', height + 'px'],//大小
            content: $("#cityWid"),
            skin: "layui-layer-molv",
            title:  "新增榜单话题"
            ,btnAlign: 'c'
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layero.find('form').find('button[lay-submit]').click();
                return false
            }
            ,btn2: function(){
                layer.closeAll();
            }
        })
    })


    //监听工具条
    table.on('tool(demo)', function(obj){
        var data = obj.data;
        if(obj.event === 'detail'){
            layer.msg('ID：'+ data.id + ' 的查看操作');
        } else if(obj.event === 'del'){
            layer.confirm('确定删除吗?', function(index){
                $.ajax({
                    url:"/clubController/delete",
                    type:"POST",
                    data:{id:data.id},
                    dataType:"json",
                    success:function(data){
                        if (data.code == -1){
                            top.location.href="/pageController/login"

                        }
                        if(data.ret ==0){
                            layer.alert('删除成功', {icon: 1}, function(index){
                                layer.closeAll("page");
                                layer.close(index);
                                location.href=location.href;
                            });
                        }else{
                            layer.alert('删除失败', {icon: 5});
                        }
                    }
                })
                layer.close(index);

            });
        } else if(obj.event === 'edit'){


            var  width=($(window).width()*0.8);
            var height=($(window).height()*0.9);
            layer.open({
                type: 1,
                closeBtn: 0,
                area: [width + 'px', height + 'px'],//大小
                content: $("#cityWid"),
                skin: "layui-layer-molv",
                title:  "话题:"+data.name
                ,btnAlign: 'c'
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    layero.find('form').find('button[lay-submit]').click();
                    return false
                }
                ,btn2: function(){
                    layer.closeAll();
                }
            })
        }
    });





    //图片上传
    //topic
    var uploadInst = upload.render({
        elem: '#uploadBanner'
        ,url: '/common/upload'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code > 0){
                return layer.msg('上传失败');
            }
            //上传成功
            $('#bannerImage').attr("src",res.url);
            $('input[name="bannerImageId"]').val(res.picId);
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });


    $(function () {
        $("#inputTopic").tagsInput();
        $("#keywordInput").tagsInput();
        // layui.formSelects.render()
        // layui.formSelects.data('city', 'server', {
        //     url: '/common/selectAllCity'
        //     ,linkage: true
        //     // ,linkageWidth: 300
        // });

        $.ajax({
            url:"/common/selectAllCity",
            type:"POST",
            dataType:"json",
            success:function(data,status,xhr){
                if(xhr.status == 200){
                    var html =""
                    $.each(eval(data),function (i,n) {
                        // console.log(n.name)
                        html += " <optgroup label="+n.name+">"
                        $.each(eval(n.children),function (i,m) {
                            // console.log(m.name)
                            html += "  <option value="+m.value+">"+m.name+"</option>"
                        })
                        html +="</optgroup>"
                    })
                    // console.log(html)
                    $("#city").append(html);
                }
            }

        })
        form.render()
        layui.formSelects.render()
    })


});





